package org.apple.sensor.monitor;

import java.util.ArrayList;
import java.util.Properties;

import org.apple.data.SensorReading;
import org.apple.data.SensorResolution;
import org.apple.databanker.*;
import org.apple.sensor.driver.*;
import org.apple.util.Configure;

public class WindSpeedSensorMonitor extends SensorMonitor {

	
	private DataBanker databanker;
	private ArrayList<SensorDriver> windSpeedSensors = new ArrayList<SensorDriver>();
	
	public WindSpeedSensorMonitor() {
		try {
			Properties prop = Configure.instance();
			databanker = (DataBanker)Class.forName(prop.getProperty("dataBankerType")).newInstance();
			sensorPeriod = Integer.parseInt(prop.getProperty("windSpeedSensorPeriod"));
			String sensorMonitorType = prop.getProperty("windSpeedSensorType");
			
			int highResNum = Integer.parseInt(prop.getProperty("windSpeedHighResCount"));
			int lowResNum = Integer.parseInt(prop.getProperty("windSpeedLowResCount"));;
			for(int i = 0; i < highResNum; i ++){
				SensorDriver driver = (SensorDriver)Class.forName(sensorMonitorType).newInstance();
				driver.init(SensorResolution.HighRes);
				try {
					driver.start();
				} catch (UninitializedException e) {
					e.printStackTrace();
				}
				windSpeedSensors.add(driver);
			}
			
			for(int i = 0; i < lowResNum; i ++){
				SensorDriver driver = (SensorDriver)Class.forName(sensorMonitorType).newInstance();
				driver.init(SensorResolution.LowRes);
				try {
					driver.start();
				} catch (UninitializedException e) {
					e.printStackTrace();
				}
				windSpeedSensors.add(driver);
			}
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	@Override
	public void monitor() {
		// TODO Auto-generated method stub
		System.out.println("Run: [WindSpeedSensorMonitor -> monitor()]");
		
		for(int i = 0; i < windSpeedSensors.size(); i++){
			try {
				databanker.write(windSpeedSensors.get(i).getSensorData(),SensorReading.SPEED);
			} catch (InvalidException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (UninitializedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

}
